var vpnserver = require('../../../../app/components/backend/util/vpnserver'),
    vpnDao = require('../../../../app/components/backend/db/daos/vpn'),
    vpnModel = require('../../../../app/components/backend/db/models/vpn'),
    assert  = require('assert'),
    Promise  = require('bluebird'),
    sandbox = require('../../../../app/components/backend/util/sandbox');

describe('app/components/backend/util/vpnserver', function() {
  beforeEach(function(){

    // 测试前置处理
    return vpnDao.findOneAndUpdate({host: '192.168.12.55'}, {host: '192.168.12.55', pwd: '111111'})
      .then(vpnDao.findOneAndUpdate.bind(vpnDao, {host: '192.168.12.100'}, {host: '192.168.12.100', pwd: '222222'}))
      .then(vpnDao.findOneAndUpdate.bind(vpnDao, {host: '192.168.12.212'}, {host: '192.168.12.212', pwd: '333333'}))
      .then(vpnserver.init)
      .then(function () {
        vpnserver.setVpnAllocation('192.168.12.55', '000');
        vpnserver.setVpnAllocation('192.168.12.100', '001');
        vpnserver.setVpnAllocation('192.168.12.212', '002');
        vpnserver.setVpnAllocation('192.168.12.55', '003');
        vpnserver.setVpnAllocation('192.168.12.212', '004');
      });
  });
  afterEach(function(){
    return  vpnModel.remove({}, function (err) {
      if (err) throw new Error(err);
      Promise.resolve();
    });
  });
  describe('#getVpn', function () {
    it('[util.vpnserver.getVpn.Nor.01].分配vpn server]. ', function () {
      assert.equal(vpnserver.getVpn('999').host, '192.168.12.100');
      assert.equal(vpnserver.getVpn('004').host, '192.168.12.212');
      assert.equal(vpnserver.getVpn('003').host, '192.168.12.55');
      return Promise.resolve(sandbox.emit(sandbox.MSG.VPN_ASSIGNED, {host: '192.168.12.212', openid: '888'}))
        .delay(100).then(function () {
          var vpnAllocation = vpnserver.getVpnAllocation();
          assert.ok(vpnAllocation['192.168.12.212'].indexOf('888') >= 0);
        });
    });

    it('[util.vpnserver.getVpn.Nor.02].没有可用的vpn]. ', function () {
      return Promise.resolve(vpnModel.remove({}, function (err) {
        if (err) throw new Error(err);
        Promise.resolve();
      }))
        .then(vpnserver.init)
        .then(sandbox.emit(sandbox.MSG.VPN_ASSIGNED, {host: '192.168.12.212', openid: '888'}))
        .delay(100).then(function () {
          assert.equal(vpnserver.getVpn('99999'), undefined);
        });
    });

    it('[util.vpnserver.Nor.02].消息.添加vpn]. ', function () {

      // 使用bluebird的Promise.resolve包裹mongoose返回的promise
      // 从而获得一些高级功能, 比如下面用到的delay
      return Promise.resolve(vpnDao.findOneAndUpdate({host: '192.168.12.220'}, {host: '192.168.12.220', pwd: '555555'}))
        .then(sandbox.emit.bind(sandbox, sandbox.MSG.VPN_ADDED, {host: '192.168.12.220'}))
        // 上面是一个不返回Promise的异步处理, 需要延迟等待处理完成
        .delay(100)
        .then(function () {
          var vpns = vpnserver.getVpnServers(),
            vpn = false;
          for (var i = 0; i < vpns.length; i++) {
            if (vpns[i].host == '192.168.12.220') {
              vpn = vpns[i];
            }
          }
          assert.ok(vpn.hasOwnProperty('_doc'));
          assert.equal(vpn.host, '192.168.12.220');
          assert.equal(vpn.pwd, '555555');
        });
    });
    it('[util.vpnserver.Nor.03].消息.修改vpn]. ', function () {
      return Promise.resolve(vpnDao.findOneAndUpdate({host: '192.168.12.55'}, {host: '192.168.12.55', pwd: '555555'}))
        .then(sandbox.emit.bind(sandbox, sandbox.MSG.VPN_RESET, {host: '192.168.12.55', pwd: '555555'}))
        .delay(100)
        .then(function () {
          var vpns = vpnserver.getVpnServers(),
              vpnAllocation = vpnserver.getVpnAllocation(),
              vpn = false;
          for (var i = 0; i < vpns.length; i++) {
            if (vpns[i].host == '192.168.12.55') {
              vpn = vpns[i];
            }
          }
          assert.ok(vpn.hasOwnProperty('_doc'));
          assert.equal(vpn.host, '192.168.12.55');
          assert.equal(vpn.pwd, '555555');
          assert.equal(vpnAllocation[vpn.host].length, 0);
        });
    });

    it('[util.vpnserver.Nor.04].消息.删除vpn]. ', function () {
      return Promise.resolve(vpnModel.remove({host: '192.168.12.55'}))
        .then(sandbox.emit.bind(sandbox, sandbox.MSG.VPN_REMOVED, {host: '192.168.12.55'}))
        .delay(100)
        .then(function () {
          var vpns = vpnserver.getVpnServers(),
              vpnAllocation = vpnserver.getVpnAllocation(),
              vpn = false;
          for (var i = 0; i < vpns.length; i++) {
            if (vpns[i].host == '192.168.12.55') {
              vpn = vpns[i];
            }
          }
          assert.equal(vpn, false);
          assert.equal(vpnAllocation['192.168.12.55'], undefined);
        });
    });
  });
});